package stutool;

import java.util.ArrayList;
import java.util.Scanner;

public class StuSystem {
public  void  StuSystemtrue(){ 
        System.out.println("欢迎来到学生信息管理系统！");  
        Scanner sc = new Scanner(System.in);
        String input = "0";
        ArrayList<stu> list = new ArrayList<>();
        printMenu();
        loop :while(true){
        System.out.print("请做出你的选择:>");
        input = sc.next();
        switch (input) {
            case "1"->addStudent(list);
            case "2"->deleteStudent(list);
            case "3"->changeStudent(list);
            case "4"->findStudent(list);
            case "5"->lookAllStudent(list);
            case "6"->{
                System.out.println("退出");
                break loop;
            }
            default->System.out.println("没有这个选项,请重新选择");
        }   


        }
        sc.close();
    }
    
//打印菜单
private static void printMenu() {
    System.out.println("-----欢迎来到学生管理系统-----");
    System.out.println("1.添加学生");
    System.out.println("2.删除学生");
    System.out.println("3.修改学生");
    System.out.println("4.查询单个学生");
    System.out.println("5.查看现有的全部学生");
    System.out.println("6.退出");

}
//添加学生
public static void addStudent(ArrayList<stu> list){
    Scanner sc = new Scanner(System.in);
    stu s = new stu();
    System.out.print("请输入要添加学生的id:>");
    String id = sc.next();
    //由于id唯一,所以要先检查id
    if(contain(list, id)){
        s.setId(id);
    }else{
        System.out.println("当前学生id重复,请重新添加");
        return;
    }
    System.out.print("请输入要添加学生的姓名:>");
    s.setName(sc.next());
    System.out.print("请输入要添加学生的年龄:>");
    s.setAge(sc.nextInt());
    System.out.print("请输入要添加学生的家庭住址:>");
    s.setHomeAddress(sc.next());
    list.add(s);
    System.out.println("学生信息添加成功！");
} 
//删除学生
public static void deleteStudent(ArrayList<stu> list){
    Scanner sc = new Scanner(System.in);
    System.out.print("请输入要删除的学生id:>");
    String id = sc.next(); 
    if(contain(list, id)){
        System.out.println("没有该学号的学生");
    }else{
        int index = getIndex(list, id);
        if(index != -1){
            list.remove(index);
            System.out.println("删除成功");
        }else{
            System.out.println("删除失败，没有该学号的学生");
        }
        
    }
}
//修改学生
public static void changeStudent(ArrayList<stu> list){
    Scanner sc = new Scanner(System.in);
    stu s = new stu();
    System.out.print("请输入要修改的学生id:>");
    String id = sc.next();
    if(contain(list, id)){
        System.out.println("没有该学号的学生");
    }else{
        s.setId(id);
        int index = getIndex(list, id);
        System.out.print("请输入该学号的新姓名:>");
        s.setName(sc.next());
        System.out.print("请输入该学号的新年龄:>");
        s.setAge(sc.nextInt());
        System.out.print("请输入该学号的新地址:>");
        s.setHomeAddress(sc.next());
        list.set(index, s); 
        System.out.println("修改成功！");
    }

    
}
//查询学生
public static void findStudent(ArrayList<stu> list){
    Scanner sc = new Scanner(System.in);
    stu s = new stu();
    System.out.print("请输入要查询的学生id:>");
    String id = sc.next();
    if(contain(list, id)){
        System.out.println("没有该学号的学生");
    }else{
        int index = getIndex(list, id);
        s = list.get(index);
        //打印表头
        System.out.println("id\t姓名\t年龄\t家庭住址");
        System.out.println(s.getId() + "\t" + s.getName() + "\t" + s.getAge() + "\t" + s.getHomeAddress());
    }
}
//查看现有全部学生
public static void lookAllStudent(ArrayList<stu> list){
    if(list.size() == 0){
        System.out.println("当前没有任何学生!");
    }else{
        //打印表头
        System.out.println("id\t姓名\t年龄\t家庭住址");
        for(int i  = 0; i < list.size(); i++){
            stu s = list.get(i);
            System.out.println(s.getId() + "\t" + s.getName() + "\t" + s.getAge() + "\t" + s.getHomeAddress());
        }
    }
}    
//检查id是否重复，存在重复返回false，不存在返回true
public static boolean contain(ArrayList<stu> list, String id){
    for(int i = 0; i < list.size(); i++){
        stu s1 = list.get(i);
        if(s1.getId().equals(id)){
            return false;//重复了
        }
    }
    return true;//没有重复
}
//通过id获得该学生在集合中的索引
public static int getIndex(ArrayList<stu> list, String id){
    for(int i = 0; i < list.size(); i++){
        if(list.get(i).getId().equals(id)){//从集合中取出第i个对像,得到它的id,再将其于形参id对比
            return i;
        }
    }
    return -1;
}

}
